﻿using CourseUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreditCalc
{

    class Payment
    {
        public int number { get; set; }
        public int mounthlyPayment { get; set; }
        public int bodyPart { get; set; }
        public int interestPart { get; set; }
        public DateTime dueDate { get; set; }
    }

     
    class Calculator
    {
        static void PrintParams(Payment[] paymentParams)
        {
            for (int i = 0; i < paymentParams.Length; i++)
            {
                Console.WriteLine("{0} {1} {2}", paymentParams[i].number, paymentParams[i].dueDate, paymentParams[i].mounthlyPayment);
            }


        }
        
        static DateTime ReadDateFromConsole(string displayText)
        {
            Console.WriteLine(displayText);
            DateTime date = new DateTime(01,01,0001);
            try
            {
                date = DateTime.Parse(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Ошибка ввода...");
                return ReadDateFromConsole(displayText);
            }
            return date;
        } 
        
        static DateTime CalculateDueDate(DateTime startDate, int paymentNumber)
        {
            DateTime dueDate = startDate;
            {
                dueDate = startDate.AddMonths(paymentNumber);
                if (dueDate.DayOfWeek == DayOfWeek.Saturday) dueDate = dueDate.AddDays(-1);
                if (dueDate.DayOfWeek == DayOfWeek.Sunday) dueDate = dueDate.AddDays(-2);
                return dueDate;

            }
                

        }

        static int CalculateMounthlyPayment(int loanSum, double interestRate, int loanTerm)
        {
            double index = (double)((interestRate / 12) * Math.Pow((1 + (interestRate / 12)),loanTerm))/((Math.Pow((1 + (interestRate / 12)),loanTerm))-1);
            int payment = (int)(loanSum * index);
            //int payment = (int)(loanSum * ((interestRate / 12) + (interestRate / 12 / ((Math.Pow((1 + (interestRate / 12)), loanTerm))) - 1)));
            return payment;
        }

        static Payment[] CalculateLoanParams (int loanSum, DateTime startDate, int loanTerm, double interestRate)
        {
            Payment[] array = new Payment[loanTerm - 1];
            
                        
            for (int i = 1; i < loanTerm; i++)
            {
                array[i-1] = new Payment();
                array[i-1].number = i;
                array[i-1].dueDate = CalculateDueDate(startDate, i);
                array[i-1].mounthlyPayment = CalculateMounthlyPayment(loanSum, interestRate, loanTerm);
                           
            }
            return array;
        }
        
        static void Main(string[] args)
        {
            DateTime date = ReadDateFromConsole("Enter start date");
            int loanSum = IOHelpers.ReadIntFromConsole("Enter loan sum");
            int loanTerm = IOHelpers.ReadIntFromConsole("Enter loan term");
            double interestRate = (IOHelpers.ReadIntFromConsole("Enter interest rate")/100);
            Payment[] paymentParams = CalculateLoanParams(loanSum,date,loanTerm,interestRate);
            PrintParams(paymentParams);
            Console.ReadKey();
        }

    }
}
